#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>  
#include "msocket.h"

SOCKET m_sock;

int NetTimeout =5000 ;//网络超时时间

int Connect( const char* address,int port)  
{  
    int rlt = 0;  
	  
    int iErrMsg;  
    //初始化WinSock  
    WSADATA wsaData;  
    iErrMsg = WSAStartup(MAKEWORD(2,2),&wsaData);  
    if (iErrMsg != 0)  
    {  
        printf("failed with wsaStartup error : %d\n",iErrMsg);  
  
        rlt = 1;  
        return rlt; 
    }  
    m_sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);  
    if (m_sock == INVALID_SOCKET)  
    {  
        printf("socket failed with error : %d\n",WSAGetLastError());  
  
        rlt = 2;  
        return rlt;  
    }  
  
    //地址配置
    SOCKADDR_IN sockaddrServer;  
    sockaddrServer.sin_family = AF_INET;  
    sockaddrServer.sin_port = htons(port);  
    sockaddrServer.sin_addr.s_addr = inet_addr(address);  
  
    setsockopt(m_sock,SOL_SOCKET,SO_RCVTIMEO,(char*)&NetTimeout,sizeof(int));
    //建立连接 
    iErrMsg = connect(m_sock,(SOCKADDR*)&sockaddrServer,sizeof(sockaddrServer)); 
    if (iErrMsg < 0)  
    {  
        printf("connect failed with error : %d\n",iErrMsg);  
  
        rlt = 3;  
        return rlt;  
    }  
	return rlt;  
}

int SendMsg(const char* msg,int len)  
{  
    int rlt = 0;  
  
    int iErrMsg = 0;  
  
    iErrMsg = send(m_sock,msg,len,0);  
    if (iErrMsg < 0)  
    {  
        printf("send msg failed with error : %d\n",iErrMsg);  
        rlt = 1;  
        return rlt;  
    }  
  
    return rlt;  
}  
unsigned int  Com_Dev_Connect( unsigned char *ip,unsigned short port, unsigned int timeout, unsigned int channel )
{
    char address[40];
    memset(address,0,40);
    sprintf(address,"%d.%d.%d.%d",ip[0],ip[1],ip[2],ip[3]);
    return Connect(address,port);
}
unsigned int Com_Dev_TxData( unsigned char *buf, unsigned int len, unsigned int timeout, unsigned int channel )
{
    unsigned int rlt = 0;  
  
    int iErrMsg = 0;  
  
    iErrMsg = send(m_sock,(char*)buf,len,0);  
    if (iErrMsg < 0)  
    {  
        printf("send msg failed with error : %d\n",iErrMsg);  
        rlt = 1;  
        return rlt;  
    } 
    printf("->send:\n");
    Com_Hook_DebugOutHex(buf,len);
    return rlt;  
}

unsigned int Com_Dev_RxData( unsigned char *buf, unsigned int *len, unsigned int bufsize, unsigned int timeout, unsigned int channel )
{
    unsigned int rcode = 0;
    int rval = 0; 
    unsigned char recvbuf[bufsize];
    int tout = 5000;
    int i =0;
    *len = 0;
    setsockopt(m_sock,SOL_SOCKET,SO_RCVTIMEO,(char*)&tout,sizeof(int));//设置超时时间
    rval = recv(m_sock,(char*)recvbuf,bufsize,0); 
    if(rval == SOCKET_ERROR)  
    {
        printf("recv socket error!\n"); 
        rcode = rval;
    }  
    else if (rval == 0)
    {
        //recv·µ»Ø0±íÊ¾Õý³£ÍË³ö  
        printf("ending connection!\n"); 
        rcode =200;
    }
    else 
    {
        memcpy(&buf[*len],recvbuf,rval);
        *len +=  rval;
        for(i = 0; i < 10; i++)
        {
            if(*len <= bufsize)
            {
                tout = 200;
                setsockopt(m_sock,SOL_SOCKET,SO_RCVTIMEO,(char*)&tout,sizeof(int));
                rval = recv(m_sock,(char*)recvbuf,bufsize-*len ,0);
                if(rval > 0) 
                {
                    memcpy(&buf[*len],recvbuf,rval);
                    *len +=  rval;
                }
            }
        }
        printf("<-recv:\n");
       
        Com_Hook_DebugOutHex(buf,*len);
        rcode =0;
    }
    return rcode;
}

unsigned int Com_Dev_TxData1( unsigned char *buf, unsigned int len, unsigned int timeout, unsigned int channel )
{
    unsigned int rlt = 0;  
  
    int iErrMsg = 0;  
  
    iErrMsg = send(m_sock,(char*)buf,len,0);  
    if (iErrMsg < 0)  
    {  
        printf("send msg failed with error : %d\n",iErrMsg);  
        rlt = 1;  
        return rlt;  
    } 
    printf("->send:\n");
    Com_Hook_DebugOutAscii(buf,len);
    return rlt;  
}

unsigned int Com_Dev_RxData1( unsigned char *buf, unsigned int *len, unsigned int bufsize, unsigned int timeout, unsigned int channel )
{
    unsigned int rcode = 0;
    int rval = 0; 
    unsigned char recvbuf[bufsize];
    int tout = 5000;
    int i =0;
    *len = 0;
    setsockopt(m_sock,SOL_SOCKET,SO_RCVTIMEO,(char*)&tout,sizeof(int));//设置超时时间
    rval = recv(m_sock,(char*)recvbuf,bufsize,0); 
    if(rval == SOCKET_ERROR)  
    {
        printf("recv socket error!\n"); 
        rcode = rval;
    }  
    else if (rval == 0)
    {
        //recv·µ»Ø0±íÊ¾Õý³£ÍË³ö  
        printf("ending connection!\n"); 
        rcode =200;
    }
    else 
    {
        memcpy(&buf[*len],recvbuf,rval);
        *len +=  rval;
        for(i = 0; i < 2; i++)
        {
            if(*len <= bufsize)
            {
                tout = 200;
                setsockopt(m_sock,SOL_SOCKET,SO_RCVTIMEO,(char*)&tout,sizeof(int));
                rval = recv(m_sock,(char*)recvbuf,bufsize-*len ,0);
                if(rval > 0) 
                {
                    memcpy(&buf[*len],recvbuf,rval);
                    *len +=  rval;
                }
            }
        }
        printf("<-recv:\n");
        Com_Hook_DebugOutAscii(buf,*len);
        rcode =0;
    }
    return rcode;
}

unsigned int Com_Dev_Disconnect( unsigned int channel )
{
    closesocket(m_sock); 
    return 0; 
}


unsigned int Com_Dev_Open( void )                                   //打开
{ return 0;}  
unsigned int Com_Dev_Close( void )                                  //关闭  
{ return 0;} 
unsigned int Com_Dev_Start( void )                                  //启动 
{ return 0;} 
unsigned int Com_Dev_Restart( void )                                    //重启 
{ return 0;} 
unsigned int Com_Dev_OSLinkProc( void )                             //连路层实时处理
{ return 0;} 
unsigned int Com_Dev_GetResource( void )                                //获取模块资源 空闲，忙
{ return 0;} 

unsigned int Com_Dev_Cfg( unsigned int commtype, unsigned int baud, unsigned char *apn, unsigned char *name, unsigned char *psd )
{ return 0;} 